package com.example.controller;

import com.example.pojo.PageResult;
import com.example.pojo.Result;
import com.example.pojo.Student;
import com.example.pojo.StudentQueryParam;
import com.example.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 学员列表数据的条件分页查询
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        log.info("分页查询，参数：{}", studentQueryParam);
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 批量删除学员信息
     * @param
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids){
        log.info("删除的学员ID：{}",ids);
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 添加学员信息
     */
    @PostMapping
    public Result add(@RequestBody Student student){
        log.info("新增的学员信息：{}",student);
        studentService.save(student);
        return Result.success();
    }

    /**
     * 根据主键ID查询学员的信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据主键ID查询的学员信息：{}",id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学员的数据信息
     */
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改的学员信息：{}",student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 该接口用于修改学员的违纪扣分
     */
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id, @PathVariable Integer score){
        log.info("修改的学员ID：{}，修改的违纪扣分：{}",id,score);
        studentService.updateViolation(id,score);
        return Result.success();
    }
}
